package net.starwifi.duobao.web.db;

import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Filter;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.DBProductService;
import net.starwifi.duobao.service.DBTimesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 夺宝
 * Created by Administrator on 2015/11/19.
 */
@Controller("apiDBMainController")
@RequestMapping("/db")
public class MainController extends BaseController {

    @Autowired
    DBProductService dbProductService;

    @Autowired
    DBTimesService dbTimesService;
    /**
     * 首页
     */
    @RequestMapping(method = RequestMethod.GET)
    public String db(Model model) {
        List<Filter> filterList=new ArrayList<Filter>();
        filterList.add(Filter.eq("isMarketable",Boolean.TRUE));
        model.addAttribute("productList",dbProductService.getMListData(filterList));
        return "/db/index";
    }

    /**
     * 首页
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("productList",dbProductService.getListData("o.isMarketable=?1", new Object[]{Boolean.TRUE}));
        return "/db/index";
    }


    /**
     * 夺宝进度(缓存)
     */
    @ResponseBody
    @RequestMapping(value = "/cQ", method = RequestMethod.GET)
    public Object cQ(String q) {
        final Map<Long,Integer> data=dbTimesService.getCQS(q);
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>(){{
            put("list", data);
        }});
    }

}
